<?php

namespace App\Services\User;


use App\Actions\PayAction;
use App\Dao\UserDao;
use App\Model\IncomeExpense;

class UserWelfareService
{
    /**
     * 用户福利
     * @param $form_uid
     * @param $from_id -来源id 订单号/收益id
     * @param $to_uid
     * @param $welfare_type 1 充值福利（金币） 2 真人认证 3 注册为男用户 4 收益 5 真人认证(自己) 6 实人认证(自己) 7学历认证(自己)
     * 收入分为：
     * 20完成任务(包括我的认证)  35推广分成
     * 支出分为：
     * 107任务支出(包括我的认证)  108推广支出
     */
    public function welfare_InvitedUsers($form_uid, $from_id, $to_uid, $welfare_type)
    {
        if (in_array($welfare_type, array(1, 2, 3, 4, 5, 6, 7)) && $to_uid > 0) {
            switch ($welfare_type) {
                case 1:
                    self::PayWelfare($form_uid, $from_id, $to_uid);
                    break;
                case 2:
                    // self::FaceAuth($form_uid, $to_uid);
                    break;
                case 3:
                    // self::RegManUser($form_uid, $to_uid);
                    break;
                case 4:
                    self::Income($form_uid, $from_id, $to_uid);
                    break;
                default:
                    // self::MyAuth($to_uid, $welfare_type);
                    break;
            }
        }
    }

    /**
     * 充值福利 - 邀请用户
     * 女 =》男：+充值的20%
     * 男 =》男：+3天SVIP
     */
    private function PayWelfare($form_uid, $from_orderid, $to_uid)
    {
        $userModel = new \UserModel();
        $from_userinfo = $userModel->findUserInfoCache($form_uid);
        $to_userinfo = $userModel->findUserInfoCache($to_uid);

        if (!empty($from_userinfo) && !empty($to_userinfo) && $from_userinfo['inviteUid'] == $to_uid) {
            $orderModel = new \OrderModel();
            $orderInfo = $orderModel->findOrderByOrderId($from_orderid);
            if (!empty($orderInfo) && $orderInfo['pay_status'] == 1) {
                //充值金币 才有分成，vip/svip 暂无分成
                if ($orderInfo['consume_type'] <= 50) {
                    $payAction = new \PayAction();
                    if ($to_userinfo['sex'] == 1 && $from_userinfo['sex'] == 1) {
                        //男 =》男：+3天SVIP
                        $giving_type = 2;//1 VIP， 2 SVIP
                        $days = PAY_MEN_MEN_SVIP;//赠送天数
                        $type = 65;//65 SVIP福利
                        $payAction->giving_vip($to_userinfo, $giving_type, $days, $type);
                    } elseif ($to_userinfo['sex'] == 2 && $from_userinfo['sex'] == 1) {
                        //女 =》男：+充值的20%
                        $giving_type = 2;//赠送积分
                        $type = 35;//邀请福利
                        $money = round($orderInfo['price']); //按金额计算
                        $rate = PAY_WOMEN_MEN_RATE;
                        $bonus = $money * GOLD_RATIO * $rate;

                        $info['ext'] = '';
                        $info['ext']->user_name = $from_userinfo['nickname'];
                        $info['ext']->type = '充值' . $money . '元';
                        $info['ext']->orderid = $from_orderid;
                        $info['ext']->earnings = $to_userinfo['sex'] == 2 ? (number_format(($bonus / GOLD_RATIO), 2) . '元') : $bonus . '金币';
                        $extend = json_encode($info['ext']);

                        if ($bonus > 0)
                            $payAction->giving_GoldOrintegrate($to_userinfo, $extend, $giving_type, $bonus, $type);
                    }
                }
            }
        }
    }

    /**
     * 收益分成 - 邀请用户
     * @param $form_uid
     * @param $from_income_id
     * @param $to_uid
     * 女=》女：收益的10%
     */
    private function Income($form_uid, $from_income_id, $to_uid)
    {
        $userDao = UserDao::getInstance();
        $from_userinfo = $userDao->findUserInfoCache($form_uid);
        $to_userinfo = $userDao->findUserInfoCache($to_uid);
        if (!empty($from_userinfo) && !empty($to_userinfo) && $from_userinfo['inviteUid'] == $to_uid) {
            $incomeModel = new IncomeExpense();
            $income_info = $incomeModel->GetIncomeInfo($from_income_id, $form_uid, 1);

            if (!empty($income_info)) {
                $payAction = new PayAction();
                $income_bonus = $income_info['amount'];
                $giving_type = 0;
                $type = 0;
                $bonus = 0;
                // $rate = $to_userinfo["spread_rate"]>0 ? $to_userinfo['spread_rate']/100 : INCOME_WOMEN_WOMEN_RATE;//收益的10%
                // 20230413 固定提成比例10%
                $rate = INCOME_WOMEN_WOMEN_RATE;

                // if ($to_userinfo['sex'] == 2 && $from_userinfo['sex'] == 2) {
                if ($from_userinfo['sex'] == 2) {
                    $bonus = $income_bonus * $rate;
                    $giving_type = 2;//积分
                    $type = 35;
                    if ($bonus > 0) {
                        $info['ext'] = '';
                        $info['ext']->user_name = $from_userinfo['nickname'];
                        $info['ext']->type = '获得收益' . $income_bonus . '积分';
                        $info['ext']->orderid = 0;
                        //预期收益
                        $info['ext']->earnings = $to_userinfo['sex'] == 2 ? (number_format(($bonus / GOLD_RATIO), 2) . '元') : $bonus . '金币';
                        $extend = json_encode($info['ext']);

                        $payAction->giving_GoldOrintegrate($to_userinfo, $extend, $giving_type, $bonus, $type, $form_uid);
                    }
                }

            }
        }
    }


    /**
     * 真人认证 - 邀请用户
     *
     * 男=》女：+50金币、+3天VIP
     * 女=》女：+30积分
     */
    private function FaceAuth($form_uid, $to_uid)
    {
        $userModel = new \UserModel();
        $from_userinfo = $userModel->findUserInfoCache($form_uid);
        $to_userinfo = $userModel->findUserInfoCache($to_uid);
        if (!empty($from_userinfo) && !empty($to_userinfo) && $from_userinfo['inviteUid'] == $to_uid) {
            $payAction = new \PayAction();
            $giving_type = 0;
            $vip_type = 0;
            $type = 0;
            $bonus = 0;
            $days = 0;

            //邀请用户 完成真人认证福利
            if ($to_userinfo['sex'] == 2 && $from_userinfo['sex'] == 1) {
                $giving_type = 1;//1 金币
                $bonus = AUTH_MAN_WOMEN_COUNT;
                $type = 1; //1 VIP，2 SVIP
                $vip_type = 55;//55 VIP福利
                $days = UTH_MAN_WOMEN_VIP;
            } elseif ($to_userinfo['sex'] == 2 && $from_userinfo['sex'] == 2) {
                $giving_type = 2;//1 积分
                $bonus = AUTH_WOMEN_WOMEN_COUNT;
            }

            if ($bonus > 0) {
                $info['ext'] = '';
                $info['ext']->user_name = $from_userinfo['nickname'];
                $info['ext']->type = '完成真人认证';
                $info['ext']->orderid = 0;
                $info['ext']->earnings = $to_userinfo['sex'] == 2 ? (number_format(($bonus / GOLD_RATIO), 2) . '元') : $bonus . '金币';
                $extend = json_encode($info['ext']);

                $payAction->giving_GoldOrintegrate($to_userinfo, $extend, $giving_type, $bonus, $type);
            }

            if ($days > 0) {
                $payAction->giving_vip($to_userinfo, $type, $days, $vip_type);
            }
        }
    }


    /**
     * 注册男用户 - 邀请用户
     * @param $form_uid
     * @param $to_uid
     * 女=》男：+30积分
     * 男=》男：+50金币
     */
    private function RegManUser($form_uid, $to_uid)
    {
        $userModel = new \UserModel();
        $from_userinfo = $userModel->findUserInfoCache($form_uid);
        $to_userinfo = $userModel->findUserInfoCache($to_uid);
        if (!empty($from_userinfo) && !empty($to_userinfo) && $from_userinfo['inviteUid'] == $to_uid) {
            $payAction = new \PayAction();
            $giving_type = 0;
            $type = 0;
            $bonus = 0;

            if ($to_userinfo['sex'] == 2 && $from_userinfo['sex'] == 1) {
                $giving_type = 2;//积分
                $bonus = REG_WOMEN_MAN_COUNT;
                $type = 35;
            } elseif ($to_userinfo['sex'] == 1 && $from_userinfo['sex'] == 1) {
                $giving_type = 1;//金币
                $bonus = REG_MAN_MAN_COUNT;
                $type = 35;
            }

            if ($bonus > 0) {
                $info['ext'] = '';
                $info['ext']->user_name = $from_userinfo['nickname'];
                $info['ext']->type = '注册成功';
                $info['ext']->orderid = 0;
                $info['ext']->earnings = $to_userinfo['sex'] == 2 ? (number_format(($bonus / GOLD_RATIO), 2) . '元') : $bonus . '金币';
                $extend = json_encode($info['ext']);

                $payAction->giving_GoldOrintegrate($to_userinfo, $extend, $giving_type, $bonus, $type);
            }
        }
    }


    /**
     * 真人认证/实人认证/学历认证 - 自己完成
     * @param $uid
     * @param $type 5 真人认证 6 实人认证 7 学历认证
     * 自己完成真人认证  +10金币/积分
     * 自己完成实人认证  +25金币/积分
     * 自己完成学历认证  +10金币/积分
     */
    private function MyAuth($uid, $type)
    {
        // \Tool::log2("MyAuth.txt", $uid, "items");
        $userModel = new \UserModel();
        $userinfo = $userModel->findUserInfoCache($uid);

        if (!empty($userinfo)) {
            $payAction = new \PayAction();
            $giving_type = 0;
            $send_type = 20;//完成任务
            $auth_name = '';

            if ($userinfo['sex'] == 2)
                $giving_type = 2;
            else
                $giving_type = 1;
            switch ($type) {
                case 5:
                    $bonus = AUTH_FACE_COUNT;
                    $auth_name = '真人认证';
                    break;
                case 6:
                    $bonus = AUTH_CARD_COUNT;
                    $auth_name = '实人认证';
                    break;
                case 7:
                    $bonus = AUTH_EDUACTION_COUNT;
                    $auth_name = '学历认证';
                    break;
            }

            if ($bonus > 0) {
                $info['ext'] = '';
                $info['ext']->user_name = $userinfo['nickname'];
                $info['ext']->type = '完成' . $auth_name;
                $info['ext']->orderid = 0;
                $info['ext']->earnings = $userinfo['sex'] == 2 ? (number_format(($bonus / GOLD_RATIO), 2) . '元') : $bonus . '金币';
                $extend = json_encode($info['ext']);

                $payAction->giving_GoldOrintegrate($userinfo, $extend, $giving_type, $bonus, $send_type);
            }
        }
    }
}